﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using ConsolidationTool.Common;
using Microsoft.SharePoint.WebControls;
using ConsolidationTool.Common.Entities;
using System.Collections.Generic;

namespace ConsolidationTool.WebParts.Flash_Input.Flash_Input_SBA.SBANewForm
{
    public partial class SBANewFormUserControl : UserControl
    {
        List<Group> regionGroup = new List<Group>();
        List<Group> subRegionGroup = new List<Group>();
        List<Group> entityGroup = new List<Group>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string level = getCurUserLevel();
                InitialForm(level);
                InitBaseInfo();
            }
        }
        void InitBaseInfo()
        {
            // lbCompanyName.Text = Constants.Company_Name;
            lbMonth.Text = DateTime.Now.Month.ToString();
            lbYear.Text = DateTime.Now.Year.ToString();

        }
        string getCurUserLevel()
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add(Constants.List_Name_Region, "ConsolidationTool_Group");
            dic.Add(Constants.List_Name_SubRegion, "ConsolidationTool_Group");
            dic.Add(Constants.List_Name_Entity, "ConsolidationTool_Group");
            List<Group> groupList = BusinessService.getGroups(dic);
            foreach (Group g in groupList)
            {
                string curUser = SPContext.Current.Web.CurrentUser.LoginName;
                bool result = BusinessService.validateUser(g.groupName, curUser);
                if (result)
                {
                    switch (g.groupLevel)
                    {
                        case Constants.Group_Level_Entity:
                            entityGroup.Add(g);
                            break;
                        case Constants.Group_Level_SubRegion:
                            subRegionGroup.Add(g);
                            break;
                        case Constants.Group_Level_Region:
                            regionGroup.Add(g);
                            break;
                    }
                }
            }
            //Save status
            ViewState["regionGroup"] = regionGroup;
            ViewState["subRegionGroup"] = subRegionGroup;
            ViewState["entityGroup"] = entityGroup;
            //
            string level = string.Empty;
            if (regionGroup.Count > 0)
                level = Constants.Group_Level_Region;
            else if (subRegionGroup.Count > 0)
                level = Constants.Group_Level_SubRegion;
            else if (entityGroup.Count > 0)
                level = Constants.Group_Level_Entity;
            return level;
        }
        void InitialForm(string level)
        {
            List<ListItem> tempList = new List<ListItem>();
            switch (level)
            {
                case Constants.Group_Level_Region:
                    //Get All Region
                    List<Region> rList = BusinessService.getRegion();
                    drpRegion.DataSource = rList;
                    drpRegion.DataValueField = "regionId"; drpRegion.DataTextField = "regionName";
                    drpRegion.DataBind();
                    //Filter Region
                    List<Group> list = (List<Group>)ViewState["regionGroup"];
                    foreach (ListItem item in drpRegion.Items)
                    {
                        string gName = rList.Find(s => s.regionName == item.Text).groupName;
                        Group g = list.Find(s => s.groupName == gName);
                        if (g == null)
                            tempList.Add(item);
                    }
                    //remove item
                    foreach (ListItem item in tempList)
                    {
                        drpRegion.Items.Remove(item);
                    }
                    drpRegion.Items.Insert(0, new ListItem("(None)", "0"));
                    //SubRegion
                    drpSubRegion.Items.Insert(0, new ListItem("(None)", "0"));
                    //Entity
                    drpEntity.Items.Insert(0, new ListItem("(None)", "0"));
                    break;
                case Constants.Group_Level_SubRegion:
                    divRegion.Visible = false;
                    List<SubRegion> sbList = BusinessService.getSubRegion();
                    drpSubRegion.DataSource = sbList;
                    drpSubRegion.DataValueField = "subRegionId"; drpSubRegion.DataTextField = "subRegionName";
                    drpSubRegion.DataBind();
                    //Filter Region
                    List<Group> slist = (List<Group>)ViewState["subRegionGroup"];
                    foreach (ListItem item in drpSubRegion.Items)
                    {
                        string gName = sbList.Find(s => s.subRegionName == item.Text).groupName;
                        Group g = slist.Find(i => i.groupName == gName);
                        if (g == null)
                            tempList.Add(item);
                    }
                    //remove item
                    foreach (ListItem item in tempList)
                    {
                        drpSubRegion.Items.Remove(item);
                    }
                    drpSubRegion.Items.Insert(0, new ListItem("(None)", "0"));
                    drpEntity.Items.Insert(0, new ListItem("(None)", "0"));
                    break;
                case Constants.Group_Level_Entity:
                    divRegion.Visible = false;
                    divSubRegion.Visible = false;
                    List<Entity> enList = BusinessService.getEntity();
                    drpEntity.DataSource = enList;
                    drpEntity.DataValueField = "CoCd"; drpEntity.DataTextField = "entityName";
                    drpEntity.DataBind();
                    //Filter 
                    List<Group> elist = (List<Group>)ViewState["entityGroup"];
                    foreach (ListItem item in drpEntity.Items)
                    {
                        string gName = enList.Find(s => s.CoCd == item.Value).groupName;
                        Group g = elist.Find(i => i.groupName == gName);
                        if (g == null)
                            tempList.Add(item);
                    }
                    //remove item
                    foreach (ListItem item in tempList)
                    {
                        drpEntity.Items.Remove(item);
                    }
                    drpEntity.Items.Insert(0, new ListItem("(None)", "0"));
                    if (drpEntity.Items.Count == 2)
                    {
                        drpEntity.SelectedIndex = 1;
                    }
                    break;
            }
        }

        protected void drpRegion_SelectedIndexChanged(object sender, EventArgs e)
        {
            drpSubRegion.DataSource = BusinessService.getSubRegion(int.Parse(drpRegion.SelectedValue));
            drpSubRegion.DataValueField = "subRegionId"; drpSubRegion.DataTextField = "subRegionName";
            drpSubRegion.DataBind();
            drpSubRegion.Items.Insert(0, new ListItem("(None)", "0"));
            drpEntity.Items.Clear();
            drpEntity.Items.Insert(0, new ListItem("(None)", "0"));
        }

        protected void drpSubRegion_SelectedIndexChanged(object sender, EventArgs e)
        {
            drpEntity.DataSource = BusinessService.getEntity(int.Parse(drpSubRegion.SelectedValue), int.Parse(drpSubRegion.SelectedValue));
            drpEntity.DataValueField = "CoCd"; drpEntity.DataTextField = "entityName";
            drpEntity.DataBind();
            drpEntity.Items.Insert(0, new ListItem("(None)", "0"));
        }

        protected void drpEntity_SelectedIndexChanged(object sender, EventArgs e)
        {
            lbCoCd.Text = drpEntity.SelectedValue;
            lbCompanyFullName.Text = Constants.Company_Name;
            //divInfo.Attributes["style"] = "font-size: small; width: 800px;";
        }


        protected void btnSave_Click(object sender, EventArgs e)
        {
            this.Page.Validate();
            if (Page.IsValid)
            {
                SPListItem item = ConsolidationTool_CoCd.ListItem;

                item["CoCd"] = lbCoCd.Text;
                item["CoFullName"] = lbCompanyFullName.Text;
                item["CoName"] = drpEntity.SelectedItem.Text;
                item["Year"] = lbYear.Text;
                item["Month"] = lbMonth.Text;
                item["Region"] = drpRegion.SelectedItem.Text;
                item["SubRegion"] = drpSubRegion.SelectedItem.Text;

                Title.ItemFieldValue = lbCoCd.Text + "_" + lbYear.Text + lbMonth.Text;
                SaveButton.SaveItem(SPContext.Current, false, string.Empty);
                Utilities.CloseForm();
            }
        }
        protected void btnExit_Click(object sender, EventArgs e)
        {
            Utilities.CloseForm();
        }
    }
}
